<?php
// 测试脚本 - 用于验证数据库连接和基本功能

require_once 'config.php';
require_once 'Database.php';

// 设置显示所有错误
error_reporting(E_ALL);
ini_set('display_errors', 1);

try {
    echo "开始测试数据库连接和基本功能...\n\n";
    
    // 测试数据库配置
    echo "1. 显示数据库配置...\n";
    echo "   DB_HOST: " . DB_HOST . "\n";
    echo "   DB_PORT: " . DB_PORT . "\n";
    echo "   DB_NAME: " . DB_NAME . "\n";
    echo "   DB_USER: " . DB_USER . "\n";
    echo "   DB_CHARSET: " . DB_CHARSET . "\n\n";
    
    // 显示PHP信息
    echo "2. 显示PHP环境信息...\n";
    echo "   PHP版本: " . phpversion() . "\n";
    echo "   MySQLi支持: " . (extension_loaded('mysqli') ? '是' : '否') . "\n";
    echo "   PDO支持: " . (extension_loaded('pdo') ? '是' : '否') . "\n";
    echo "   PDO MySQL支持: " . (extension_loaded('pdo_mysql') ? '是' : '否') . "\n\n";
    
    // 测试数据库连接
    echo "3. 测试数据库连接...\n";
    $pdo = getDbConnection();
    echo "   数据库连接成功！\n\n";
    
    // 测试用户权限
    echo "4. 测试用户权限...\n";
    try {
        $stmt = $pdo->prepare("SHOW GRANTS FOR CURRENT_USER()");
        $stmt->execute();
        $grants = $stmt->fetchAll();
        echo "   用户权限信息:\n";
        foreach ($grants as $grant) {
            echo "     " . reset($grant) . "\n";
        }
        echo "\n";
    } catch (Exception $e) {
        echo "   无法获取用户权限信息: " . $e->getMessage() . "\n\n";
    }
    
    // 测试数据库是否存在
    echo "5. 测试数据库是否存在...\n";
    try {
        $stmt = $pdo->prepare("SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ?");
        $stmt->bindValue(1, DB_NAME);
        $stmt->execute();
        $dbExists = $stmt->fetch();
        if ($dbExists) {
            echo "   数据库 " . DB_NAME . " 存在\n\n";
        } else {
            echo "   数据库 " . DB_NAME . " 不存在\n\n";
        }
    } catch (Exception $e) {
        echo "   无法检查数据库是否存在: " . $e->getMessage() . "\n\n";
    }
    
    // 测试数据库操作类
    echo "6. 测试数据库操作类...\n";
    $db = new Database();
    echo "   数据库操作类创建成功！\n\n";
    
    // 测试获取项目列表
    echo "7. 测试获取项目列表...\n";
    $projects = $db->getProjectList(1, 5);
    echo "   项目列表获取成功，共有 " . $projects['count'] . " 个项目。\n";
    echo "   当前页项目数量: " . count($projects['list']) . "\n\n";
    
    // 测试保存项目
    echo "8. 测试保存项目...\n";
    $testFlag = 'test-project-' . time();
    $result = $db->saveProject(
        $testFlag,
        '测试项目',
        '这是一个测试项目',
        '{"bgm":null,"script":null,"pages":[{"name":"测试页面","eIndex":0,"style":{"backgroundColor":"#ffffff"},"elements":[]}]}'
    );
    
    if ($result) {
        echo "   项目保存成功！\n\n";
        
        // 验证项目是否保存成功
        echo "9. 测试获取单个项目...\n";
        $project = $db->getProjectByFlag($testFlag);
        if ($project) {
            echo "   项目获取成功！项目标题: " . $project['title'] . "\n";
            echo "   项目ID: " . $project['id'] . "\n\n";
            
            // 测试通过ID获取项目
            echo "10. 测试通过ID获取项目...\n";
            $projectById = $db->getProjectById($project['id']);
            if ($projectById) {
                echo "   通过ID获取项目成功！项目标题: " . $projectById['title'] . "\n\n";
            } else {
                echo "   通过ID获取项目失败！\n\n";
            }
        } else {
            echo "   项目获取失败！\n\n";
        }
    } else {
        echo "   项目保存失败！\n\n";
    }
    
    echo "测试完成！\n";
    
} catch (Exception $e) {
    error_log("测试失败: " . $e->getMessage());
    echo "测试失败: " . $e->getMessage() . "\n";
    echo "文件: " . $e->getFile() . "\n";
    echo "行号: " . $e->getLine() . "\n";
    
    // 显示异常追踪
    echo "追踪信息:\n";
    echo $e->getTraceAsString() . "\n";
}